package com.wandian.game.sound
{		import flash.events.*;
		
		public class RandomSound extends EventDispatcher
		{
			private var fSounds_sndex_arr:Array;
			private var fSoundsProbabilities_num_arr:Array;
			private var fNextSoundToPlay_sndex:SoundEx;
			private var fCurrentSound_sndex:SoundEx;
			
			public function RandomSound()
			{
				this.fSounds_sndex_arr = new Array();
				this.fSoundsProbabilities_num_arr = new Array();
				return;
			}// end function
			
			public function playSound(param1:Number = 0, param2:Number = 0) : SoundChannelEx
			{
				var _loc_3:SoundChannelEx = null;
				if (!this.fNextSoundToPlay_sndex)
				{
					return null;
				}
				_loc_3 = this.fNextSoundToPlay_sndex.play(param1, 0, param2);
				this.fCurrentSound_sndex = this.fNextSoundToPlay_sndex;
				if (param2 > 0)
				{
					_loc_3.addEventListener(SoundChannelEx.EVENT_SEAMLESS_STOP_STARTED, this.dispatchEvent);
				}
				_loc_3.addEventListener(SoundChannelEx.EVENT_SOUND_CHANNEL_PLAYING_COMPLETED, this.dispatchEvent, false, 1);
				this.fCurrentSound_sndex.addEventListener(SoundEx.EVENT_SOUND_PLAYING_COMPLETED, this.dispatchEvent);
				this.fNextSoundToPlay_sndex = this.selectSoundToPlay();
				return _loc_3;
			}// end function
			
			protected function get soundProbabilities() : Array
			{
				if (!this.fSoundsProbabilities_num_arr)
				{
					this.fSoundsProbabilities_num_arr = new Array();
				}
				return this.fSoundsProbabilities_num_arr;
			}// end function
			
			public function set soundOn(param1:Boolean) : void
			{
				var _loc_2:int = 0;
				_loc_2 = 0;
				while (_loc_2 < this.fSounds_sndex_arr.length)
				{
					
					SoundEx(this.fSounds_sndex_arr[_loc_2]).soundOn = param1;
					_loc_2++;
				}
				return;
			}// end function
			
			protected function normalizeSoundsProbabilitiesIfRequired() : void
			{
				var _loc_1:* = undefined;
				var _loc_2:int = 0;
				_loc_1 = 0;
				_loc_2 = 0;
				while (_loc_2 < this.soundProbabilities.length)
				{
					
					_loc_1 = _loc_1 + this.soundProbabilities[_loc_2];
					_loc_2++;
				}
				if (_loc_1 <= 1)
				{
					return;
				}
				_loc_2 = 0;
				while (_loc_2 < this.soundProbabilities.length)
				{
					
					this.soundProbabilities[_loc_2] = this.soundProbabilities[_loc_2] / _loc_1;
					_loc_2++;
				}
				return;
			}// end function
			
			protected function registerSoundProbability(param1:Number) : void
			{
				this.soundProbabilities.push(param1);
				this.normalizeSoundsProbabilitiesIfRequired();
				return;
			}// end function
			
			public function stopSound(param1:Number = 0) : void
			{
				if (this.fCurrentSound_sndex)
				{
					this.fCurrentSound_sndex.stop(param1);
				}
				return;
			}// end function
			
			public function addSound(param1:String, param2:Number = 1, param3:Number = 1, param4:Number = 0) : SoundEx
			{
				var _loc_5:SoundEx = null;
				_loc_5 = new SoundEx(param1, param3, param4);
				this.fSounds_sndex_arr.push(_loc_5);
				this.registerSoundProbability(param2);
				this.fNextSoundToPlay_sndex = this.selectSoundToPlay();
				return _loc_5;
			}// end function
			
			private function selectSoundToPlay() : SoundEx
			{
				var _loc_1:Number = NaN;
				var _loc_2:Number = NaN;
				var _loc_3:int = 0;
				_loc_1 = Math.random();
				_loc_2 = 0;
				_loc_3 = 0;
				while (_loc_3 < this.fSoundsProbabilities_num_arr.length)
				{
					
					_loc_2 = _loc_2 + Number(this.fSoundsProbabilities_num_arr[_loc_3]);
					if (_loc_1 <= _loc_2)
					{
						return this.fSounds_sndex_arr[_loc_3];
					}
					_loc_3++;
				}
				return null;
			}// end function
			
		}
	}
